package com.tool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tool.util.TemplateUtil;
import com.tool.views.Column;
import com.tool.views.TableInfo;
import com.tool.views.TableView;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Test {

	private final static Logger LOGGER = LoggerFactory.getLogger(Test.class);

	public static void main(String[] args) throws Exception {

		Template vueTpl = TemplateUtil.getTemplate("vue.ftl");
		Template svTpl = TemplateUtil.getTemplate("service.ftl");
		Template toolTpl = TemplateUtil.getTemplate("convert.ftl");
		Template queryTpl = TemplateUtil.getTemplate("query.ftl");
		Template svImplTpl = TemplateUtil.getTemplate("serviceImpl.ftl");
		Template jsTpl = TemplateUtil.getTemplate("api.js.ftl");
		Template acTpl = TemplateUtil.getTemplate("controller.ftl");
		List<TableView> tables = Lists.newArrayList();

		TableView admin_resources0 = new TableView();
		admin_resources0.setTableName("passenger");
		admin_resources0.setAlias("category");
		admin_resources0.setDescription("问卷类别管理");
		admin_resources0.setForms(Sets.newHashSet("title", "description", "sort"));
		admin_resources0.setSearchs(Sets.newHashSet("title", "description"));
		admin_resources0.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
		tables.add(admin_resources0);

//		TableView admin_resources1 = new TableView();
//		admin_resources1.setTableName("paper");
//		admin_resources1.setAlias("paper");
//		admin_resources1.setDescription("问卷管理");
//		admin_resources1.setForms(Sets.newHashSet("title", "description", "category_id", "sort"));
//		admin_resources1.setSearchs(Sets.newHashSet("title", "description", "category_id"));
//		admin_resources1.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources1);
//
//		TableView admin_resources2 = new TableView();
//		admin_resources2.setTableName("paper_question");
//		admin_resources2.setAlias("question");
//		admin_resources2.setDescription("题目管理");
//		admin_resources2.setForms(Sets.newHashSet("title", "description", "sort", "category_id", "type"));
//		admin_resources2.setSearchs(Sets.newHashSet("title", "description", "category_id", "type"));
//		admin_resources2.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources2);
//
//		TableView admin_resources3 = new TableView();
//		admin_resources3.setTableName("paper_question_option");
//		admin_resources3.setAlias("questionOption");
//		admin_resources3.setDescription("题目选项管理");
//		admin_resources3.setForms(Sets.newHashSet("question_id", "type", "content", "description", "sort", "serial"));
//		admin_resources3.setSearchs(Sets.newHashSet("question_id", "type", "content", "description"));
//		admin_resources3.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources3);
//
//		TableView admin_resources4 = new TableView();
//		admin_resources4.setTableName("paper_score");
//		admin_resources4.setAlias("paperScore");
//		admin_resources4.setDescription("问卷答案管理");
//		admin_resources4.setForms(Sets.newHashSet("paper_id", "min_score", "max_score", "conclusion"));
//		admin_resources4.setSearchs(Sets.newHashSet("paper_id", "min_score", "max_score", "conclusion"));
//		admin_resources4.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources4);
//
//		TableView admin_resources5 = new TableView();
//		admin_resources5.setTableName("sys_register_ext");
//		admin_resources5.setAlias("registerExt");
//		admin_resources5.setDescription("注册信息表单管理");
//		admin_resources5
//				.setForms(Sets.newHashSet("title", "description", "sort", "attr_name", "regular", "must", "type"));
//		admin_resources5
//				.setSearchs(Sets.newHashSet("title", "description", "sort", "attr_name", "regular", "must", "type"));
//		admin_resources5.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources5);
//
//		TableView admin_resources6 = new TableView();
//		admin_resources6.setTableName("sys_register_ext_option");
//		admin_resources6.setAlias("registerExtOption");
//		admin_resources6.setDescription("注册信息表单选项管理");
//		admin_resources6.setForms(Sets.newHashSet("register_ext_id", "content", "description", "sort"));
//		admin_resources6.setSearchs(Sets.newHashSet("register_ext_id", "content", "description", "sort"));
//		admin_resources6.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources6);
//
//		TableView admin_resources7 = new TableView();
//		admin_resources7.setTableName("user");
//		admin_resources7.setAlias("user_answer");
//		admin_resources7.setDescription("用户账户管理");
//		admin_resources7.setForms(Sets.newHashSet("nickname", "account", "password", "vip"));
//		admin_resources7.setSearchs(Sets.newHashSet("nickname", "account", "password", "vip"));
//		admin_resources7.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources7);
//
//		TableView admin_resources8 = new TableView();
//		admin_resources8.setTableName("user_answer");
//		admin_resources8.setAlias("userAnswer");
//		admin_resources8.setDescription("用户答题信息管理");
//		admin_resources8.setForms(Sets.newHashSet("page_data", "journey"));
//		admin_resources8.setSearchs(Sets.newHashSet("user_id", "page_id", "page_data", "journey", "status"));
//		admin_resources8.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources8);
//
//		TableView admin_resources9 = new TableView();
//		admin_resources9.setTableName("user_answer_option");
//		admin_resources9.setAlias("userAnswerOption");
//		admin_resources9.setDescription("用户答题选项管理");
//		admin_resources9.setForms(Sets.newHashSet("user_id"));
//		admin_resources9.setSearchs(Sets.newHashSet("user_id", "answer_id", "question_id"));
//		admin_resources9.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources9);
//
//		TableView admin_resources10 = new TableView();
//		admin_resources10.setTableName("user_ext");
//		admin_resources10.setAlias("userExt");
//		admin_resources10.setDescription("用户信息管理");
//		admin_resources10.setForms(Sets.newHashSet("text"));
//		admin_resources10.setSearchs(Sets.newHashSet("text", "user_id"));
//		admin_resources10.setIngores(Sets.newHashSet("ctime", "mtime", "deleted"));
//		tables.add(admin_resources10);
		for (TableView table : tables) {

			String tableName = table.getTableName();

			List<String> names = DatabaseUtil.getColumnNames(tableName);
			List<String> types = DatabaseUtil.getColumnTypes(tableName);
			List<String> descriptions = DatabaseUtil.getColumnComments(tableName);

			TableInfo info = new TableInfo();
			info.setTableName(tableName);
			info.setModular("biz");
			info.setPermPrefix("biz");

			info.setQueryModel(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table.getAlias()) + "Query");
			info.setDaoPackage("com.alikill.survey");
			info.setServicePackage("com.alikill.survey.biz.service");
			info.setActionPackage("com.alikill.survey.controller.biz");
			info.setToolsPackage("com.alikill.survey.biz.tools");
			info.setQueryPackage("com.alikill.survey.biz.query");

			info.setRootRouter("api");
			info.setAlias(table.getAlias());
			info.setDescription(table.getDescription());
			info.setMapper(info.getAlias());

			info.setMenuName(tableName.trim().toUpperCase());
			List<Column> columns = Lists.newArrayList();
			List<Column> forms = Lists.newArrayList();
			List<Column> searchs = Lists.newArrayList();
			List<Column> tableColums = Lists.newArrayList();

			Set<String> formList = table.getForms();
			Set<String> searchList = table.getSearchs();
			Set<String> ingoreList = table.getIngores();

			if (CollectionUtils.isEmpty(formList)) {
				formList = Sets.newHashSet();
			}
			if (CollectionUtils.isEmpty(searchList)) {
				searchList = Sets.newHashSet();
			}
			for (int index = 0; index < names.size(); index++) {

				String name = names.get(index);
				String type = types.get(index);
				String description = descriptions.get(index);

				Column column = new Column();
				column.setName(name);
				column.setDescription(description);
				column.setJdbcType(type);

				columns.add(column);
				if (formList.contains(name)) {
					forms.add(column);
				}
				if (searchList.contains(name)) {
					searchs.add(column);
				}
				if (!ingoreList.contains(name)) {
					tableColums.add(column);
				}
				info.setTables(tableColums);
				info.setColumns(columns);
				info.setSearchs(searchs);
				info.setForms(forms);
			}

			/** VUE */
			createFile(vueTpl, info, "vue", (info.getAlias().trim().toLowerCase() + ".vue"));

			/** JS */
			createFile(jsTpl, info, "js", (info.getAlias().trim().toLowerCase() + ".js"));

			/** Controller */
			String acName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(acTpl, info, "controller", (acName + "Controller.java"));

			/** IService */
			String svName = "I" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(svTpl, info, info.getPermPrefix() + "/service", (svName + "SV.java"));

			/** ServiceImpl */
			String svImplName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
			createFile(svImplTpl, info, info.getPermPrefix() + "/service" + "/impl", (svImplName + "SVImpl.java"));

//			/** toolTpl */
//			String toolName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, info.getTableName());
//			createFile(toolTpl, info, info.getPermPrefix() + "/tools", (toolName + "Tools.java"));

			/** queryTpl */
			String queryName = info.getQueryModel();
			createFile(queryTpl, info, info.getPermPrefix() + "/query", (queryName + ".java"));

		}
	}

	private static void createFile(Template tpl, TableInfo info, String path, String fileName)
			throws IOException, TemplateException {
		String dirPath = "D:/auto/" + path + "/";
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File jsfile = new File(dirPath, fileName);
		Writer writer = new FileWriter(jsfile);
		tpl.process(info, writer);
		writer.close();

		LOGGER.info(fileName + " 文件创建完成");
	}
}
